﻿using CustomModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace LibrarySystem.Tools
{
    /// <summary>
    /// 设置 参数key
    /// </summary>
    public enum SettingParamEnum
    {
        HistoryCount,//堆栈历史最大次数 int
        StackItemNewPanel,//打开堆栈的页面都是新的 bool
        TreeOpenNewPanel,//树形图打开的页面都是新的 bool 
        PageOpenNewPanel,//页面内打开的新页面都是新的 bool
        MouseEnterOpenMenu,//鼠标进入菜单标头打开菜单 bool  true 表示打开 ，否则表示不打开,默认是false

    }

    /// <summary>
    /// 全局设置类
    /// </summary>
    public class GlobalSetting
    {
        private static GlobalSetting _instance;

        /// <summary>
        /// 全部设置参数
        /// </summary>
        private Dictionary<SettingParamEnum, string> _settingParam;

        private GlobalSetting()
        {
            this._settingParam = new Dictionary<SettingParamEnum, string>();
            _initParm();
        }

        public static GlobalSetting GetGlobalSetting()
        {
            if (_instance==null)
            {
                _instance = new GlobalSetting();
            }
            return _instance;
        }


        public delegate void SendMessageDelegate(string message);

        public event SendMessageDelegate SendMessageEvent;

        public void OpenTipMessage(string content)
        {
            if (this.SendMessageEvent!=null)
            {
                this.SendMessageEvent(content);
            }
        }

        /// <summary>
        /// 设置 设置参数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>设置成功返回true，否则返回false</returns>
        public bool SetSetting(SettingParamEnum key,string value)
        {
            try
            {
                this._settingParam[key] = value;
                XmlHelper.SetXmlNodeAttributeValue(GlobalPath.GetCustomConfigPath() + GlobalParam.user + ".xml", "root", key.ToString(), value.ToString());

                return true;
            }
            catch (Exception)
            {

                return false;
            }
          
        }

        /// <summary>
        /// 初始化字典
        /// </summary>
        private void _initParm()
        {
            this._settingParam.Add(SettingParamEnum.HistoryCount, "10");
            this._settingParam.Add(SettingParamEnum.TreeOpenNewPanel, "true");
            this._settingParam.Add(SettingParamEnum.StackItemNewPanel, "false");
            this._settingParam.Add(SettingParamEnum.PageOpenNewPanel, "true");
            this._settingParam.Add(SettingParamEnum.MouseEnterOpenMenu, "true");
        }

        /// <summary>
        /// 根据key 获得属性值
        /// </summary>
        /// <param name="key"></param>
        /// <returns>返回值的字符串格式</returns>
        public string GetSetting(SettingParamEnum key)
        {
            string res = XmlHelper.GetXmlNodeAttributeValue(GlobalPath.GetCustomConfigPath() + GlobalParam.user + ".xml", "root", key.ToString());
            if (string.IsNullOrWhiteSpace(res))
            {
                return this._settingParam[key];
            }
            return res;

           
        }


       
    }
}
